package hebClustering.web;

import java.io.*;

import hebClustering.Cluster;
import hebClustering.vectorSpace.DocumentVector;
import hebClustering.vectorSpace.IVector;
import hebClustering.vectorSpace.MetricSpace;

/**
 * A class that formats search results to HTML format.
 * 
 */
public class HTMLExport {

	private Writer writer;

	/**
	 * Creates a new HTMLExport object.
	 * 
	 * @param query - The query that was searched.
	 * 
	 * @throws IOException
	 */
	public HTMLExport(String query) throws IOException{

		FileOutputStream fos = new FileOutputStream("result.html");
		writer = new OutputStreamWriter(fos, "Windows-1255");

		String output = "";
		output += "<html dir=\"RTL\">\n";

		output += "<head>\n";
		output += "<meta charset=windows-1255>";
		output += "<title>אִשְׁכּוּל בְּעִבְרִית - תוצאות</title>\n";
		output += "</head>\n";

		output += "<body style=\"font-family:arial\";>\n\n";
		output += "<b><u><h1 style=\"font-size:250%;\";><a name=\"top\">תוצאות - &quot;" + query + "&quot;</a></h1></u></b>\n";

		writer.write(output);
	}

	/**
	 * Format the search results from a metric space to a HTML string.
	 * 
	 * @param space - The metric space that holds the clustered search results.
	 * @throws IOException
	 */
	public void metricSpaceToHTML(MetricSpace space) throws IOException{
		
		String output = "<h3>פרטי המרחב:</h3>";
		output += "<p>";
		output += "<u>" + "מימד: " + "</u>" + space.getDimension() + "<br />\n";
		output += "<u>" + "מספר תוצאות: " + "</u>"  + space.getVectors().size() + "<br />\n";
		output += "<u>" + "מספר אשכולות: " + "</u>"  + space.getClusters().size() + "<br />\n";
		output += "<u>" + "איכות אשכול כוללת: " + "</u>"  + space.getClusters().quality() + "<br />\n";

		int i = 1;

		output += "<h3>אשכולות: </h3>";

		output += "<p>";
		for (int j = 1; j <= space.getClusters().size(); j++){
			output += "<a href=\"#C" + j + "\">" + j + "</a> ";
		}
		output += "</p><br />";

		for (Cluster c : space.getClusters()){

			output += "<b><i><a name=\"C"+i+"\">" + "אשכול מספר " + i + "</a></b></i>"+ "<br />\n";
			i++;
			output += "<u>" + "נציג:" + "</u> ";
			DocumentVector rep = (DocumentVector)c.getRepresentative();
			if (rep.getInfo().getTitle() == null){
				output += "<a href = \""+ rep.getInfo().getUrl() +"\" target=\"_blank\"> " + rep.getInfo().getUrl().substring(rep.getInfo().getUrl().lastIndexOf('\\')+1) + "</a>";
			}else{
				output += "<a href = \""+ rep.getInfo().getUrl() +"\" target=\"_blank\"> " + rep.getInfo().getTitle() + "</a>";
				if (rep.getInfo().getDescription() != null) output += "<br />\n" + rep.getInfo().getDescription();
			}
			output += "<br />\n";
			output += "<u>" + "גודל: " + "</u>" + c.size() + "<br />\n";
			output += "<u>" + "איכות: " + "</u>" + c.getSSE() + "<br />\n";

			output += "<ul>";
			for (IVector v : c){
				DocumentVector dv = (DocumentVector)v;
				if (dv.getInfo().getTitle() == null)
					output += "<a href = \""+ dv.getInfo().getUrl() +"\" target=\"_blank\"> " + dv.getInfo().getUrl().substring(dv.getInfo().getUrl().lastIndexOf('\\')+1) + "</a>";
				else{
					output += "<a href = \""+ dv.getInfo().getUrl() +"\" target=\"_blank\"> " + dv.getInfo().getTitle() + "</a>";
					if (dv.getInfo().getDescription() != null) output += "<br />\n" + dv.getInfo().getDescription();
				}

				output += "<br />\n";
			}
			output += "</ul>";

			output += "<a href=\"#top\">חזור למעלה</a>";

			output += "<hr />\n";
		}

		output += "<br /><br /><p align=center style=\"font-size:74%\">כל הזכויות שמורות לשי בן-משה, רן צווליק ורומן ציפרון &copy;</p>";
		output += "<img src=\"http://www.dispatchesfromchina.com/images/powered_by_Bing.jpg\" alt=\"powered by Bing\" align=left width=\"100\" height=\"20\" />";
		output += "</body>\n</html>\n";

		writer.write(output);
		writer.close();
	}

}
